#!/usr/bin/python

#-------------------------------------------------------------------------------------------------
# Name:        map
# Purpose:     map class to do various operations
#
# Author:      cackles
#
# Created:     06/25/2011
#-------------------------------------------------------------------------------------------------



import getopt
from localtypes import Position, Logger
import random
import sys
import os
from pygame import sprite
         

#
# use filter() for calling a function multiple times over a list (returning elements for return=true)
#  filter(collision, listofobjects)
#

class mapsquare(sprite.RenderUpdates):
    def __init__(self, x=0, y=0, z=0):
        sprite.RenderUpdates.__init__(self)

class terrain(Position, sprite.DirtySprite):
    def __init__(self, x=0, y=0, z=0):
        Position.__init__(self, x, y, z)
        sprite.DirtySprite.__init__(self)
        
          
    def __repr__(self):
        hierarchy = list(self.__class__.__mro__)
        hierarchy.reverse() 
        z = str(hierarchy[-1])
        z = z.split(' ')[-1].strip("<'>")
        z = z.split('.')[-1]
        return '%16s(%d,%d,%d)' % (z, self.x, self.y, self.z)


class mountain(terrain):
    def __init__(self, x=0, y=0, z=0):
        terrain.__init__(self, x, y, z)  
        self.imagefile = "images" + os.sep + "mountain.png"
    
class rock(terrain):
    def __init__(self, x=0, y=0, z=0):
        terrain.__init__(self, x, y, z)  
        self.imagefile = "images" + os.sep + "rock.png"
    
class desert(terrain):
    def __init__(self, x=0, y=0, z=0):
        terrain.__init__(self, x, y, z)  
        self.imagefile = "images" + os.sep + "desert.png"

class forest(terrain):
    def __init__(self, x=0, y=0, z=0):
        terrain.__init__(self, x, y, z)  
        self.imagefile = "images" + os.sep + "forest.png"

class forest2(terrain):
    def __init__(self, x=0, y=0, z=0):
        terrain.__init__(self, x, y, z)  
        self.imagefile = "images" + os.sep + "forest2.png"

class forest3(terrain):
    def __init__(self, x=0, y=0, z=0):
        terrain.__init__(self, x, y, z)  
        self.imagefile = "images" + os.sep + "forest3.png"
                
class ocean(terrain):
    def __init__(self, x=0, y=0, z=0):
        terrain.__init__(self, x, y, z)  
        self.imagefile = "images" + os.sep + "ocean.png"

class map(Logger):
    locations = []
    def __init__(self, rows=0, columns=0):
        self.rows = rows
        self.columns = columns
        self.locations = [ [ 0 for col in range(self.columns) ] for row in range(self.rows) ]
        random.seed(1)
        self.Generate()

    def Generate(self):
        for r in range(0, self.rows):
            for c in range(0, self.columns):
                t_type = random.randint(0,2)
                if t_type == 0:
                    self.locations[r][c] = forest(c, r, 0)
                elif t_type == 1:
                    self.locations[r][c] = rock(c, r, 0)
                else:
                    self.locations[r][c] = mountain(c, r, 0)                                        
        
        self.Print()
    
    def Print(self):
        for r in range(0, self.rows):
            for c in range(0, self.columns):
                print self.locations[r][c],
            print ""
    
    def FlatMap(self):
        return self.iter_flatten(self.locations)
        
    def iter_flatten(self, iterable):  
        it = iter(iterable)
        for e in it:
            if isinstance(e, (list, tuple)):
                for f in self.iter_flatten(e):
                    yield f
            else:
                yield e
    

        


def usage():
    print "Usage: [-h] [-p <pieces>]"
    print " Required:"
    print "  -p,--pieces <pieces>               Number of pieces on the map"
    print " Optional:"
    print "  -h,--help                          Print this usage information"

# main method
def main():
    pieces = 0

    try:
        # set up the arguments available to the user
        opts, args = getopt.getopt(sys.argv[1:], "p:h", ["pieces=", "help"])

    except getopt.GetoptError, err:
        print str(err)
        usage()
        sys.exit(1)

    # loop through the arguments passed by the user
    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("-p", "--pieces"):
            pieces = a
        else:
            assert False, "unhandled option"
            usage()

    if pieces == 0:
        usage()
        sys.exit()

    map()

    sys.exit()

if __name__ == '__main__':
    main()
